package com.bookmanger.server.service.impl;

import com.bookmanger.common.exception.BaseException;
import com.bookmanger.common.result.Result;
import com.bookmanger.pojo.DTO.CheckInfoDTO;
import com.bookmanger.pojo.Entity.AdminEntity;
import com.bookmanger.pojo.Entity.Reader;
import com.bookmanger.server.mapper.SysAdminMapper;
import com.bookmanger.server.service.EmailService;
import com.bookmanger.server.service.SysAdminService;
import com.bookmanger.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SysAdminServiceImpl implements SysAdminService {
    @Autowired
    private SysAdminMapper sysAdminMapper;
    @Autowired
    private EmailService emailService;


    //检查manager表中CheckInfoDTO中的<content,type>是否已存在
    @Override
    public void checkInfo(CheckInfoDTO checkInfoDTO) {
        String findInfo;
        findInfo = sysAdminMapper.checkInfo(checkInfoDTO);
        if(findInfo!=null){
            String errorMessage = String.format("%s已经存在!", checkInfoDTO.getType());
            throw new BaseException(errorMessage);
        }
    }


    @Override
    public Result deleteAdmin(Integer id) {
        sysAdminMapper.deleteAdmin(id);
        return Result.success("删除成功");
    }

    @Override
    public Result updateAdminInfo(AdminEntity adminEntity) {
        AdminEntity isExist = sysAdminMapper.findAdminById(adminEntity.getId());
        if (isExist == null){
            return Result.error("管理员不存在");
        }
        sysAdminMapper.updateAdminInfo(adminEntity);
        return Result.success("更新成功");
    }

    //根据Id查看Admin详情
    @Override
    public Result findAdminById(Integer id) {
        AdminEntity adminEntity = sysAdminMapper.findAdminById(id);
        if (adminEntity == null){
            return Result.error("管理员不存在");
        }
        return Result.success(adminEntity);
    }

    @Override
    public Result findAllAdmin() {
        List<AdminEntity> adminEntities =sysAdminMapper.findAllAdmin();
        return Result.success(adminEntities);
    }

    @Override
    public Result findAllAdmin(Integer page, Integer pageSize) {
        // 参数校验
        if (page == null || page < 1) page = 1;
        if (pageSize == null || pageSize < 1) pageSize = 10;
        if (pageSize > 100) pageSize = 100;
        
        PageHelper.startPage(page, pageSize, true);
        List<AdminEntity> adminEntities = sysAdminMapper.findAllAdmin();
        PageInfo<AdminEntity> pageInfo = new PageInfo<>(adminEntities);
        return Result.success(pageInfo);
    }

    @Override
    public Result findAdminByInfo(String info) {
        List<AdminEntity> adminEntities = sysAdminMapper.findAdminByInfo(info);
        return Result.success("查询成功", adminEntities);
    }

    @Override
    public Result addAdmin(AdminEntity adminEntity) {
        //检查是否已存在
        checkInfo(CheckInfoDTO.builder().info(adminEntity.getAccount()).type("account").build());
        //密码加密，添加普通管理员
        adminEntity.setPassword(MD5Util.getMD5String(adminEntity.getPassword()));
        adminEntity.setType(0);
        sysAdminMapper.addAdmin(adminEntity);
        return Result.success("管理员添加成功");
    }

    @Override
    public Result findAllReader() {
        List<Reader> readers = sysAdminMapper.findAllReader();
        return Result.success("查询成功", readers);
    }

    @Override
    public Result findAllReader(Integer page, Integer pageSize) {
        // 参数校验
        if (page == null || page < 1) page = 1;
        if (pageSize == null || pageSize < 1) pageSize = 10;
        if (pageSize > 100) pageSize = 100;
        
        PageHelper.startPage(page, pageSize, true);
        List<Reader> readers = sysAdminMapper.findAllReader();
        PageInfo<Reader> pageInfo = new PageInfo<>(readers);
        return Result.success("查询成功", pageInfo);
    }

    @Override
    public Result findReaderById(Integer id) {
        Reader reader = sysAdminMapper.findReaderById(id);
        if (reader == null){
            return Result.error("用户不存在");
        }
        return Result.success("查询成功", reader);
    }

    @Override
    public Result deleteReader(Integer id) {
        sysAdminMapper.deleteReader(id);
        return Result.success("删除成功");
    }

    @Override
    public Result updateReaderInfoByAdmin(Reader reader) {
        Reader isExist = sysAdminMapper.findReaderById(reader.getId());
        if (isExist == null){
            return Result.error("用户不存在");
        }
        sysAdminMapper.updateReaderInfoByAdmin(reader);
        return Result.success("更新成功");
    }

    @Override
    public Result aggreeRegister(Integer id,Integer type) {
        Reader reader = sysAdminMapper.findReaderById(id);
        if (reader == null){
            return Result.error("用户不存在");
        }
        if (reader.getStatus() == 1){
            return Result.error("用户已通过审核");
        }
        emailService.sendEmailSuccess(reader.getEmail());
        sysAdminMapper.aggreeRegister(id,type);
        return Result.success("审核成功");
    }

    @Override
    public Result findReaderByInfo(String info) {
        List<Reader> readers = sysAdminMapper.findReaderByInfo(info);
        return Result.success("查询成功", readers);
    }

    @Override
    public Result disaggressRegister(Integer id,String reason) {
        Reader reader = sysAdminMapper.findReaderById(id);
        if (reader == null){
            return Result.error("用户不存在");
        }
        emailService.sendEmailUnsuccess(reader.getEmail(), reason);
        sysAdminMapper.deleteReader(id);
        return Result.success("拒绝成功");
    }


}